<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <link rel="stylesheet" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
<title>CJAX Web Service Unit Tests</title>
  <script>
  $(document).ready(function(){
    function checkSearchResults(data,checkValue,correctValue) {
    	var search = "";
        for (var i = 0; i < data.length; i++) {
        	if (data[i].bibrecord = correctValue) {
        		search = data[i].bibrecord;
        	}
        }
        equals(search,correctValue,"Veryifying Search Results " + checkValue); 
    }
    function checkStraightHit(data,checkValue,correctValue) {
        var search = "";
        for (var i = 0; i < data.length; i++) {
        	if (data[i].bibrecord = correctValue) {
        		search = data[i].bibrecord;
        	}
        }
        equals(search,correctValue,"Verifying Straight Hit " + checkValue);
    }
    function checkNoResults(data,checkValue,correctValue) {
        var search = "";
        for (var i = 0; i < data.length; i++) {
        	if (data[i].bibrecord = correctValue) {
        		search = data[i].bibrecord;
        	}
        }
        equals(search,correctValue,"Verifying No Results " + checkValue);
    }

    var opactests = [{ edition: "vt", revision: "", catalogIndex: 0, check: [ 
        {method: checkSearchResults, searchString: "keyword", searchValue: "freakonomics", correctValue: "2651126"},
        {method: checkStraightHit, searchString: "keyword", searchValue: "superfreakonomics", correctValue: "2691325"},
        {method: checkNoResults, searchString: "keyword", searchValue: "blahblah", correctValue: ""}
        ] 
    },
    { edition: "vt", revision: "1", catalogIndex: 0, check: [ 
        {method: checkSearchResults, searchString: "keyword", searchValue: "freakonomics", correctValue: "2651126"},
        {method: checkStraightHit, searchString: "keyword", searchValue: "superfreakonomics", correctValue: "2691325"},
        {method: checkNoResults, searchString: "keyword", searchValue: "blahblah", correctValue: ""}
        ] 
    }];

var url = "http://libx.lib.vt.edu/services/cjax/"

for (var k = 0; k < opactests.length; k++) { 
for (var i = 0; i < opactests[k].check.length; i++) (function(k,i){
    var edition = opactests[k];
    var revision = "";
    if (edition.revision) {
        revision = ".";
    }
    module(edition.edition + revision + edition.revision);
    var check = edition.check[i];
    test("Checking " + check.searchString + " for value " + check.searchValue, function () {
         stop();
         $.getJSON(url + edition.edition + revision + edition.revision + "/" + edition.catalogIndex + "/"+ check.searchString +"/" + check.searchValue, function(data) {
                    check.method(data.searchResults,check.searchValue,check.correctValue);
                    //equals(0,0);
                    start();
                });

    });
})(k,i);
}

  });
  </script>
  
</head>
<body>
  <h1 id="qunit-header">CJAX Web Service Unit Tests</h1>
 <h2 id="qunit-banner"></h2>
 <h2 id="qunit-userAgent"></h2>
 <ol id="qunit-tests"></ol>
</body>
</html>
